<template>
  <div class="login-wrapper">
    <div class="title">
      <span class="email">{{ status }}</span>
      <span class="other">唯有春风最相惜</span>
    </div>
    <van-form v-if="state === 2" label-width="80px" @submit="onSubmit">
      <van-field
        v-model="formData.email"
        name="用户名"
        label="用户名"
        placeholder="请输入邮箱账号"
        clearable
        :rules="[{ required: true, message: '请填写邮箱账号' }]"
      />
      <van-field
        v-model="formData.password"
        :type="passwordType"
        name="密码"
        label="密码"
        placeholder="会员密码"
        :rules="[{ required: true, message: '请填写会员密码' }]"
      >
        <template #button>
          <van-icon
            name="eye-o"
            @click="
              passwordType === 'password'
                ? (passwordType = 'text')
                : (passwordType = 'password')
            "
          />
        </template>
      </van-field>

      <van-field name="radio" label="登录有效期">
        <template #input>
          <van-radio-group v-model="formData.savetime" direction="horizontal">
            <van-radio name="0">保持一次</van-radio>
            <van-radio name="1">保持一天</van-radio>
            <van-radio name="7">保持七天</van-radio>
            <van-radio name="30">保持一月</van-radio>
          </van-radio-group>
        </template>
      </van-field>
      <div style="margin: 20px 10px">
        <van-button
          round
          block
          type="info"
          native-type="submit"
        >提交</van-button>
      </div>
    </van-form>
    <register v-else-if="state === 1" />
    <update v-else-if="state === 3" @goto="state = 2" />
    <forget v-else-if="state === 4" />
    <div class="fun">
      <div v-if="state !== 1" @click="state = 1">邮箱注册</div>
      <div v-if="state !== 2" @click="state = 2">邮箱登录</div>
      <div v-if="state !== 3" @click="state = 3">修改密码</div>
      <div v-if="state !== 4" @click="state = 4">忘记密码</div>
    </div>
  </div>
</template>

<script>
import update from './update'
import forget from './forget'
import register from './register'
import { mapActions, mapMutations } from 'vuex'
export default {
  components: { update, forget, register },
  data() {
    return {
      formData: {
        email: '407126588@qq.com',
        password: '123',
        savetime: '1'
      },
      passwordType: 'password',
      status: '邮箱登录',
      // 1: 注册 2：登录 3：修改密码 4：忘记密码
      state: 2
    }
  },
  watch: {
    state() {
      switch (this.state) {
        case 1:
          this.status = '邮箱注册'
          break
        case 2:
          this.status = '邮箱登录'
          break
        case 3:
          this.status = '修改密码'
          break
        case 4:
          this.status = '忘记密码'
          break
      }
    }
  },
  methods: {
    onSubmit() {
      this.login(this.formData).then((res) => {
        if (res.data.ok === 1) {
          this.userinfo({ userName: this.formData.email }).then((res) => {
            if (res.data.ok === 1) {
              this.setUserinfo(res.data.data)
            }
          })
        }
      })
    },
    ...mapMutations(['setUserinfo']),
    ...mapActions(['login', 'userinfo'])
  }
}
</script>

<style lang="scss" scoped>
.login-wrapper {
  width: 100%;
  .title {
    display: flex;
    justify-content: space-between;
    height: 80px;
    padding: 0 20px;
    .email {
      flex: 1;
      height: 100%;
      line-height: 80px;
      text-align: left;
      font-size: 18px;
      font-weight: 700;
    }
    .other {
      flex: 0 0 auto;
      line-height: 100px;
      height: 100%;
      color: #aaa;
      font-size: 12px;
    }
  }
  .van-form {
    padding: 0 10px;
  }
  .van-radio {
    margin-bottom: 12px;
  }
  .fun {
    width: 100%;
    height: 60px;
    display: flex;
    padding: 0 24px;
    justify-content: space-between;
    margin-top: 40px;
    line-height: 60px;
    font-size: 14px;
  }
  .van-field {
    margin-bottom: 10px;
  }
}
</style>
